// API响应基础类型
export interface ApiResponse<T = any> {
  code: number
  message: string
  data: T
  timestamp: number
  success: boolean
}

// 分页请求参数
export interface PageQuery {
  current: number
  size: number
}

// 分页响应数据
export interface PageResponse<T = any> {
  current: number
  size: number
  total: number
  pages: number
  records: T[]  // 实际后端返回的是 records 字段
}

// 用户相关类型
export interface LoginRequest {
  username: string
  password: string
  tenantId?: string
  loginType?: string
  captcha?: string  // ✅ 与后端字段名一致
  captchaKey?: string
  rememberMe?: boolean  // ✅ 添加后端已有的字段
}

export interface RegisterRequest {
  username: string
  password: string
  confirmPassword: string
  nickname: string
  email: string
  emailCode: string        // 邮箱验证码（必填）
  phone?: string
  tenantId?: string
  captcha?: string
  captchaKey?: string
  agreedToTerms?: boolean
}

export interface LoginResponse {
  accessToken: string
  refreshToken: string
  expiresIn: number
  sessionId?: string
  permVersion?: number
  userInfo: UserInfo
  authInfo?: AuthInfo  // ✅ 权限信息（可选，如果后端返回则可减少额外API调用）
}

// 权限信息结构
export interface AuthInfo {
  roles: RoleInfo[]
  permissions: string[]
  menus?: MenuItem[]
  dataScope?: any
}

export interface UserInfo {
  userId: string
  username: string
  nickname: string
  email?: string
  phone?: string
  avatarUrl?: string
  tenantId: string
  deptId?: string
  status: number
  roles: RoleInfo[]
  permVersion?: number
  lastLoginTime?: string
  createTime?: string
}

export interface RoleInfo {
  roleId: string
  roleCode: string
  roleName: string
}

// 权限相关类型
export interface PermissionInfo {
  permissionId: string
  permissionCode: string
  permissionName: string
  permissionType: string
  status: number
}

// 菜单类型 - 与后端Entity完全一致
export interface MenuItem {
  id: string
  parentId?: string
  name: string
  title: string
  path: string
  component?: string
  icon?: string
  orderNo: number
  visible: boolean
  menuType: string  // DIR-目录 MENU-菜单 BUTTON-按钮
  permission?: string
  isCache?: boolean
  children?: MenuItem[]
}

export interface MenuMeta {
  title: string
  icon?: string
  keepAlive: boolean
  ignoreAuth: boolean
  permissions?: string[]
}

export interface UserMenuResponse {
  menus: MenuItem[]
  permissions: string[]
  roles: string[]
  totalMenus: number
}

// 用户管理相关类型
export interface UserDTO {
  userId?: string
  username: string
  nickname?: string
  email?: string
  phone?: string
  avatar?: string
  avatarUrl?: string  // 后端返回的字段名
  deptId?: string
  status?: number
  roles?: RoleInfo[]  // 用户角色列表
  createTime?: string
  updateTime?: string
}

export interface UserQueryRequest extends PageQuery {
  username?: string
  nickname?: string
  status?: number
  deptId?: string
}

// 角色管理相关类型
export interface RoleDTO {
  roleId?: string
  roleCode: string
  roleName: string
  dataScope?: string
  customDeptIds?: string[]
  status?: number
  remark?: string
  createTime?: string
  updateTime?: string
}

export interface RoleQueryRequest extends PageQuery {
  roleCode?: string
  roleName?: string
  status?: number
}

// 权限管理相关类型
export interface PermissionDTO {
  permissionId?: string
  permissionCode: string
  permissionName: string
  permissionType?: string
  type?: string
  status?: number
  category?: string
  description?: string
  menuId?: string        // 关联的菜单ID
  menuName?: string      // 菜单名称（用于显示）
  createTime?: string
  updateTime?: string
}

export interface PermissionQueryRequest extends PageQuery {
  permissionType?: string
  permissionCode?: string
  permissionName?: string
  menuId?: string
  status?: number
}

// 菜单管理相关类型
export interface MenuDTO {
  // 后端字段（主导）
  menuId?: string
  parentMenuId?: string
  menuName?: string
  menuTitle?: string
  routePath?: string
  componentPath?: string
  menuIcon?: string
  menuType?: string // M/C/F
  permissionCode?: string
  orderNo?: number
  isFrame?: boolean
  isCache?: boolean
  visible?: boolean
  status?: number
  createdAt?: string
  updatedAt?: string
  childMenus?: MenuDTO[]

  // 兼容旧字段（部分前端历史代码可能使用）
  parentId?: string
  name?: string
  title?: string
  path?: string
  component?: string
  icon?: string
  permission?: string
  createTime?: string
  updateTime?: string
  children?: MenuDTO[]
}

export interface MenuQueryRequest extends PageQuery {
  menuType?: string
  menuName?: string
  status?: number
}

// 租户管理相关类型
export interface TenantDTO {
  id?: string
  tenantId?: string
  tenantName: string
  tenantType?: 'ENTERPRISE' | 'STANDARD' | 'TRIAL'
  status?: number
  statusText?: string
  maxUsers?: number
  expireTime?: string
  contactName?: string
  contactPhone?: string
  contactEmail?: string
  description?: string
  userCount?: number
  nearExpiry?: boolean
  createdBy?: string
  createdAt?: string
  updatedBy?: string
  updatedAt?: string
  deleted?: number
}

export interface TenantQueryRequest extends PageQuery {
  tenantName?: string
  status?: number
  tenantType?: string
}

export interface TenantStatsDTO {
  tenantId: string
  userCount: number
  roleCount: number
  activeUserCount: number
  lastActiveTime?: string
  canAddUsers: boolean
  remainingQuota: number
}

export interface TenantOptionDTO {
  tenantId: string
  tenantName: string
  status: number
}
